package com.akuhome.resource.util;

import com.akuhome.resource.enums.mapper.CodeBasedEnum;

import java.util.ArrayList;
import java.util.List;

public class CodeEnumUtil {
    public static <E extends CodeBasedEnum> E codeOf(Class<E> enumClass, int code) {
        E[] enumConstants = enumClass.getEnumConstants();
        for (E e : enumConstants) {
            if (e.getCode() == code)
                return e;
        }
        return null;
    }

    public static <E extends CodeBasedEnum> List<E> codeOf(Class<E> enumClass, int[] code) {
        List<E> res = new ArrayList<>();
        E[] enumConstants = enumClass.getEnumConstants();
        for(int c:code){
            for (E e : enumConstants) {
                if (e.getCode() == c)
                    res.add(e);
            }
        }
        return res;
    }
}
